<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/11/011
 * Time: 8:57
 */

namespace app\project\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Power extends Base
{

    /**
     * 列表
     */
    public function index(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['name'] = ['like',"%{$keyword}%"];
        }

        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $thisWechat = Session::get("thisWechat");
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = ['gt',0];
                $list = Db::name("project_power")->where($where)->limit($start,$limit)->order("id desc")->select();
                foreach ($list as $k=>$v){
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $count = Db::name("project_power")->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("index");
    }

    /**
     * 详情
     */
    public function detail(){
        $thisWechat = Session::get("thisWechat");
        $id = empty($_POST['id'])?0:$_POST['id'];
        if(!empty($id)){
            $where['id'] = $id;
            $where['uniacid'] = $thisWechat['id'];
            $detail = Db::name("project_power")->where($where)->find();
            $this->assign('detail',$detail);
        }

        $template = empty($_POST['id'])?"addView":"saveView";
        return view($template);
    }

    /**
     * 添加操作
     */
    public function add(){
        $thisWechat = Session::get("thisWechat");
        try{
            $rule = [
                ["name", "require", "权限名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['addtime'] = time();
            $data['add_id'] = Session::get("id");
            Db::name("project_power")->insert($data);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改操作
     */
    public function save(){
        $thisWechat = Session::get("thisWechat");
        try{
            $rule = [
                ["name", "require", "权限名称不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $data['addtime'] = time();
            $data['add_id'] = Session::get("id");
            Db::name("project_power")->update($data);
            $black['code'] = 0;
            $black['msg'] = "修改成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除操作
     */
    public function del(){
        try{
            $id = $_POST['id'];
            $power = Db::name("project_power")->where(['id'=>['in',$id]])->select();
            $res = Db::name("project_power")->where(['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            $common = new Common();
            foreach ($power as $k=>$v){
                $log['remark'] = "账号【" . Session::get("user_name") . "】删除权限【".$v['name']."】id【".$v['id']."】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}